/**
 * Copyright (C), 2015-2018, 辰森世纪
 * FileName: StringUtils
 * Author:   syc
 * Date:     2018/8/2 13:37
 * Description: 自定义字符串工具类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.clouddo.common.utils;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @classDesc: 功能描述:(字符串工具类)
 * @author: xinjinlong
 * @date: 2018/12/16 11:50
 * @copyright (C), 2015-2018, 辰森世纪
 * @email: xjl@choicesoft.com.cn
 */
public class StringUtil {
    private static final Pattern UPPER_CASE_PATTERN = Pattern.compile("[A-Z]");

    public static boolean isEmpty(String value) {
        return (value == null) || value.isEmpty();
    }

    public static boolean isNotEmpty(String value) {
        return !isEmpty(value);
    }

    public static String trimToNull(String string) {
        if (string == null) {
            return null;
        }
        String returnString = string.trim();
        if (returnString.isEmpty()) {
            return null;
        } else {
            return returnString;
        }
    }

    public static String join(Collection<String> collection, String delimiter) {
        return join(collection, delimiter, new ToStringFormatter());

    }

    public static String join(Collection collection, String delimiter, StringUtilsFormatter formatter) {
        if (collection == null) {
            return null;
        }

        if (collection.isEmpty()) {
            return "";
        }

        StringBuffer buffer = new StringBuffer();
        for (Object val : collection) {
            buffer.append(formatter.toString(val)).append(delimiter);
        }

        String returnString = buffer.toString();
        return returnString.substring(0, returnString.length() - delimiter.length());
    }

    public interface StringUtilsFormatter<Type> {
        String toString(Type obj);
    }

    public static class ToStringFormatter implements StringUtilsFormatter {
        @Override
        public String toString(Object obj) {
            if (obj == null) {
                return null;
            }
            return obj.toString();
        }
    }

    /**
     * 如果字符串包含大写字母，则将大写字母转换为"_"+对应小写字母
     * @param value
     * @return
     */
    public static String transCamelToLineAndLowerCase(String value) {
        if (isEmpty(value)) {
            return value;
        }

        Matcher matcher = UPPER_CASE_PATTERN.matcher(value);
        StringBuffer stringBuffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(stringBuffer, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(stringBuffer);
        return stringBuffer.toString();
    }

    /**
     * 如果字符串包含大写字母，则将大写字母转换为"_"+对应小写字母
     * @param value
     * @return
     */
    public static String transCamelToLineAndUpperCase(String value) {
        if (isEmpty(value)) {
            return value;
        }

        Matcher matcher = UPPER_CASE_PATTERN.matcher(value);
        StringBuffer stringBuffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(stringBuffer, "_" + matcher.group(0));
        }
        matcher.appendTail(stringBuffer);
        return stringBuffer.toString().toUpperCase();
    }
}
